
// Copyright (C) 2012 Luca Piccioni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//  
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//  
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Xml;

namespace Derm.Xml.Collada
{
	/// <summary>
	/// Object representing Collada element 'asset/contributor' (contributor).
	/// </summary>
	public class ColladaAssetConstributor : XmlSerializableExt
	{
		#region Collada Asset Contributor Information

		/// <summary>
		/// Contributor name.
		/// </summary>
		public string Author;

		/// <summary>
		/// Contributor authoring tool.
		/// </summary>
		public string AuthoringTool;

		/// <summary>
		/// Contributor comment.
		/// </summary>
		public string Comments;

		/// <summary>
		/// Contributor copyright.
		/// </summary>
		public string Copyright;

		#endregion

		#region XmlSerializableExt Overrides

		/// <summary>
		/// Read an element of the current XML serializable.
		/// </summary>
		/// <param name="elementName">
		/// A <see cref="System.String"/> that specify the element name.
		/// </param>
		/// <param name="xmlReader">
		/// A <see cref="XmlReader"/> used for reading child element. It is positioned at the start element.
		/// </param>
		/// <remarks>
		/// <para>
		/// This method is responsible to read the start element of the nested element.
		/// </para>
		/// <para>
		/// This method is resposible to read all children elements till the corresponding end element, without reading it. In the
		/// case this condition is not satisfied, the XML parsing will be aborted.
		/// </para>
		/// </remarks>
		public override void ReadElement(string elementName, XmlReader xmlReader)
		{
			if        (elementName == ColladaElementAuthor) {
				Author = XmlHelper.ReadXmlTextElement(xmlReader);
			} else if (elementName == ColladaElementAuthoringTool) {
				AuthoringTool = XmlHelper.ReadXmlTextElement(xmlReader);
			} else if (elementName == ColladaElementComments) {
				Comments = XmlHelper.ReadXmlTextElement(xmlReader);
			} else if (elementName == ColladaElementCopyright) {
				Copyright = XmlHelper.ReadXmlTextElement(xmlReader);
			} else
				XmlHelper.SkipXmlElement(xmlReader);
		}

		/// <summary>
		/// Collada 'asset/constributor' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementAuthor = "author";

		/// <summary>
		/// Collada 'asset/constributor' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementAuthoringTool = "authoring_tool";

		/// <summary>
		/// Collada 'asset/constributor' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementComments = "comments";

		/// <summary>
		/// Collada 'asset/constributor' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementCopyright = "copyright";

		#endregion
	}

	/// <summary>
	/// Object representing Collada element 'asset/unit' (unit).
	/// </summary>
	public class ColladaAssetUnit : XmlSerializableExt
	{
		#region Collada Asset Unit Information

		/// <summary>
		/// Ratio factor to convert meters to unit.
		/// </summary>
		public float Meters = 1.0f;

		/// <summary>
		/// Unit name.
		/// </summary>
		public string Name = "meter";

		#endregion

		#region XmlSerializableExt Overrides

		/// <summary>
		/// Read the attribute of the current XML serializable.
		/// </summary>
		/// <param name="xmlReader">
		/// A <see cref="XmlReader"/> used for reading XML element attributes.
		/// </param>
		/// <remarks>
		/// <para>
		/// This method is called only in the case there are effectively attributes on XML element. Don't expect that this
		/// method will be called even when the attributes count is zero. This is very important, since the design doesn't
		/// allow object initialization using this method.
		/// </para>
		/// </remarks>
		public override void ReadAttributes(XmlReader xmlReader)
		{
			string attribute;

			if ((attribute = xmlReader[ColladaAttributeMeter]) != null)
				Meters = Single.Parse(attribute);
			if ((attribute = xmlReader[ColladaAttributeName]) != null)
				Name = attribute;
		}

		/// <summary>
		/// Collada 'asset/unit' attribute name (1.4).
		/// </summary>
		private static readonly string ColladaAttributeMeter = "meter";

		/// <summary>
		/// Collada 'asset/unit' attribute name (1.4).
		/// </summary>
		private static readonly string ColladaAttributeName = "name";

		#endregion
	}

	/// <summary>
	/// Object representing Collada element 'asset'
	/// </summary>
	public class ColladaAsset : XmlSerializableExt
	{
		#region Collada Asset Information

		/// <summary>
		/// Asset contributor(s).
		/// </summary>
		public List<ColladaAssetConstributor> Contributors = new List<ColladaAssetConstributor>();

		/// <summary>
		/// Creation date and time.
		/// </summary>
		public DateTime Created;

		/// <summary>
		/// Modification date and time.
		/// </summary>
		public DateTime Modified;

		/// <summary>
		/// Revision information.
		/// </summary>
		public string Revision;

		/// <summary>
		/// Subject information.
		/// </summary>
		public string Subject;

		/// <summary>
		/// Title string.
		/// </summary>
		public string Title;

		/// <summary>
		/// Keyword used as search criteria for the parent element..
		/// </summary>
		public string Keywords;

		/// <summary>
		/// Unit information.
		/// </summary>
		public ColladaAssetUnit Unit = new ColladaAssetUnit();

		/// <summary>
		/// Up axis.
		/// </summary>
		public ColladaUpAxis UpAxis = ColladaUpAxis.UpAxisY;

		#endregion

		#region XmlSerializableExt Overrides

		/// <summary>
		/// Read an element of the current XML serializable.
		/// </summary>
		/// <param name="elementName">
		/// A <see cref="System.String"/> that specify the element name.
		/// </param>
		/// <param name="xmlReader">
		/// A <see cref="XmlReader"/> used for reading child element. It is positioned at the start element.
		/// </param>
		/// <remarks>
		/// <para>
		/// This method is responsible to read the start element of the nested element.
		/// </para>
		/// <para>
		/// This method is resposible to read all children elements till the corresponding end element, without reading it. In the
		/// case this condition is not satisfied, the XML parsing will be aborted.
		/// </para>
		/// </remarks>
		public override void ReadElement(string elementName, XmlReader xmlReader)
		{
			if (elementName == ColladaElementContributor) {
				ColladaAssetConstributor contributor = new ColladaAssetConstributor();

				contributor.ReadXml(xmlReader);

				Contributors.Add(contributor);
			} else if (elementName == ColladaElementCreated) {
				Created = XmlHelper.ReadXmlDateTime(xmlReader);
			} else if (elementName == ColladaElementKeywords) {
				Keywords = XmlHelper.ReadXmlTextElement(xmlReader);
			} else if (elementName == ColladaElementModified) {
				Modified = XmlHelper.ReadXmlDateTime(xmlReader);
			} else if (elementName == ColladaElementRevision) {
				Revision = XmlHelper.ReadXmlTextElement(xmlReader);
			} else if (elementName == ColladaElementSubject) {
				Subject = XmlHelper.ReadXmlTextElement(xmlReader);
			} else if (elementName == ColladaElementTitle) {
				Title = XmlHelper.ReadXmlTextElement(xmlReader);
			} else if (elementName == ColladaElementUnit) {
				Unit = new ColladaAssetUnit();
				Unit.ReadXml(xmlReader);
			} else if (elementName == ColladaElementUpAxis) {
				XmlHelper.SkipXmlElement(xmlReader);				// XXX
			}
		}

		/// <summary>
		/// Collada 'asset' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementContributor = "contributor";

		/// <summary>
		/// Collada 'asset' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementCreated = "created";

		/// <summary>
		/// Collada 'asset' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementKeywords = "keywords";

		/// <summary>
		/// Collada 'asset' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementModified = "modified";

		/// <summary>
		/// Collada 'asset' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementRevision = "revision";

		/// <summary>
		/// Collada 'asset' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementSubject = "subject";

		/// <summary>
		/// Collada 'asset' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementTitle = "title";

		/// <summary>
		/// Collada 'asset' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementUnit = "unit";

		/// <summary>
		/// Collada 'asset' child element name (1.4).
		/// </summary>
		private static readonly string ColladaElementUpAxis = "up_axis";

		#endregion
	}
}
